﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace project
{
    public partial class BookPage : UIPage
    {
        public static Action updateDataGridView;

        public BookPage()
        {
            InitializeComponent();
            this.Size = new System.Drawing.Size(1271, 631);
            this.uiDataGridView1.Size = new System.Drawing.Size(1201, 561);
            uiDataGridView1.AllowUserToAddRows = false;
            uiDataGridView1.ReadOnly = true;
            uiDataGridView1.MultiSelect = false;
            uiDataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            uiDataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }
        private void page_Initialize(object sender, EventArgs e)
        {

        }

        private void uiTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(this.uiTextBox1.Text))
            {
                MessageBox.Show("请输入正确查询内容", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void BookPage_Load(object sender, EventArgs e)
        {
            updateDataGridView = QueryAllBook;
            QueryAllBook();
        }
        
        protected void QueryAllBook()
        {
            string sqlStr = "select * from books";

            // 把DataGridView的数据源绑定到DataSet上的第一个表中
            uiDataGridView1.DataSource = SqlHelper.ExcuteDataSet(sqlStr).Tables[0];

            // 设置表格上显示的列标题
            uiDataGridView1.Columns[0].HeaderText = "序号";
            uiDataGridView1.Columns[1].HeaderText = "书号";
            uiDataGridView1.Columns[2].HeaderText = "书名";
            uiDataGridView1.Columns[3].HeaderText = "作者";
            uiDataGridView1.Columns[4].HeaderText = "出版社";
            uiDataGridView1.Columns[5].HeaderText = "库存";
            uiDataGridView1.Columns[6].HeaderText = "总量";
            uiDataGridView1.Columns[7].HeaderText = "馆藏地点";
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            QueryAllBook();
        }

        private void uiButton1_Click_1(object sender, EventArgs e)
        {
            string str1 = string.Empty;

            switch (uiComboBox1.Text)
            {
                case "书号":
                    str1 = "number";
                    break;
                case "书名":
                    str1 = "name";
                    break;
                case "作者":
                    str1 = "author";
                    break;
                case "出版社":
                    str1 = "publisher";
                    break;
                default:
                    str1 = "";
                    break;
            }

            if (string.IsNullOrEmpty(uiComboBox1.Text))
            {
                QueryAllBook();
            }
            else if (!string.IsNullOrEmpty(str1))
            {
                string sqlStr = $"select * from books where {str1} like '%{uiTextBox1.Text}%'";

                uiDataGridView1.DataSource = SqlHelper.ExcuteDataSet(sqlStr).Tables[0];

                // 设置表格上显示的列标题
            }

        }

        private void uiTextBox1_Load(object sender, EventArgs e)
        {
            updateDataGridView = QueryAllBook;
            QueryAllBook();
        }

        private void BookPage_Initialize(object sender, EventArgs e)
        {

        }

        private void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
